<template>
  <div class="login-wrap">
    <div class="bg-box">
      <img src="./logo.png" class="logo" onclick="return false"/>
      <div class="title">蓝聘</div>
      <div class="tab-box">
        <div class="btn login" @click="showLogin">登录</div>
        <div class="btn regist">注册</div>
      </div>
    </div>

    <!--注册-->
    <div class="regist-box">
      <div class="wrap-box">
        <div class="icon1"><img src="../../images/phone.png" class="phone1"/></div>
        <div class="input-box"><input type="text" class="phone2" placeholder="请输入手机号码" maxlength="11"
                                      v-model="phone"
                                      v-on:input="checkIcon" @blur="inputBlur"/>
        </div>
        <div class="icon2" v-if="showIcon"><img src="../../images/icon_1.png" class="phone3" onclick="return false"/>
        </div>
        <div class="icon2" v-if="hideIcon"><img src="../../images/icon_2.png" class="phone3" onclick="return false"/>
        </div>
      </div>
      <div class="wrap-box">
        <div class="icon3"><img src="../../images/pwd.png" class="pwd1"/></div>
        <div class="pwd-box"><input type="password" :type="pwdType" class="pwd2" placeholder="请输入6-16位密码"
                                    maxlength="16"
                                    v-model="password" @blur="inputBlur"/></div>
        <div class="icon4" @click="changeType"><img :src=pwdIcon class="pwd3" onclick="return false"/></div>
      </div>
      <div class="wrap-box distance">
        <div class="icon3"><img src="../../images/code.png" class="pwd1"/></div>
        <div class="code-box"><input type="text" class="code" placeholder="请输入验证码" maxlength="6" v-model="codeNum"
                                     @blur="inputBlur"/>
        </div>
        <div class="icon5">
          <timer-btn ref="timerBtn" class="btn" v-on:run="sendCode"></timer-btn>
        </div>
      </div>
      <div class="opt-box">
        <div class="select-box"><input type="checkbox" class="checkbox" :class="{'select-icon': choice}"
                                       @click="checkItem"/></div>
        <div class="protocol" @click="protocol">我已阅读，并同意《用户注册协议》</div>
      </div>
      <div class="btn-box">
        <button type="button" class="sure" @click="register">注册</button>
      </div>
    </div>
  </div>
</template>

<script>
  import md5 from 'js-md5'
  import {Base64} from 'js-base64'
  import code from '../../components/code/code'
  import {baseUrl} from "../../common/js/env"
  import {getUrlKey, setSessionStore, getSessionStore, removeSessionStore} from "../../common/js/util"

  export default {
    data() {
      return {
        phone: "",
        password: "",
        codeNum: "",
        token1: "",
        token2: "",
        choice: true,
        showIcon: false,
        hideIcon: false,
        pwdType: "password",
        pwdIcon: require("../../images/icon_4.png"),
        identity: getUrlKey("identity"),
        id: "",
        interviewId: ""
      }
    },
    mounted() {
      if (getSessionStore("account")) {
        this.phone = Base64.decode(getSessionStore("account"));
      }
      this.id = this.$route.query.id;
      this.interviewId = this.$route.query.interviewId;
      this.getIdentity();
    },
    methods: {
      checkItem() {
        this.choice = !this.choice;
      },
      showLogin() {
        removeSessionStore("account");
        if (this.id) {
          this.$router.push({path: '/login', query: {"id": this.id}});
        } else if (this.interviewId) {
          this.$router.push({path: '/login', query: {"interviewId": this.interviewId}});
        } else {
          this.$router.push({path: "/login", query: {"identity": this.identity}});
        }
      },
      checkIcon() {
        let phone = this.phone;
        let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (!reg.test(phone.trim())) {
          this.showIcon = false;
          this.hideIcon = true;
        } else {
          this.showIcon = true;
          this.hideIcon = false;
        }
      },
      changeType() {
        this.pwdType = this.pwdType === 'password' ? 'text' : 'password';
        this.pwdIcon = this.pwdIcon == require("../../images/icon_3.png") ? require("../../images/icon_4.png") : require("../../images/icon_3.png");
      },
      checkPwd() {//校验密码
        let pwd = this.password;
        if (pwd == "") {
          this.$vux.toast.text("密码不能为空", "middle");
          return;
        } else if (pwd.indexOf(" ") >= 0) {
          this.$vux.toast.text("密码中不能有空格字符", "middle");
          return;
        } else if ((/[\u4e00-\u9fa5]+/).test(pwd)) {
          this.$vux.toast.text("密码不能包含中文", "middle");
          return;
        } else if (pwd.length < 6 || pwd.length > 16) {
          this.$vux.toast.text("密码长度为6-16位");
          return;
        }
        return true;
      },
      checkPhone() {//校验手机号码
        let phone = this.phone;
        var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (phone == "") {
          this.$vux.toast.text("手机号码不能为空", "middle");
          return;
        }
        if (!reg.test(phone)) {
          this.$vux.toast.text("手机号码格式错误", "middle");
          return;
        }
        return true;
      },
      checkCode() {//校验验证码
        let reg = /^[0-9]*$/;
        let code = this.codeNum;
        if (code == "") {
          this.$vux.toast.text("验证码不能为空", "middle");
          return;
        } else if (!reg.test(code)) {
          this.$vux.toast.text("验证码格式不正确", "middle");
          return;
        }
        return true;
      },
      sendCode() {//获取验证码
        let phone = this.phone;
        if (this.checkPhone() && this.checkPwd()) {
          this.$refs.timerBtn.setDisabled(true);//设置按钮不可用
          this.axios.get(baseUrl + "/login/get_token?phone=" + phone + "&i=0").then((res) => {
            let resultObj = res.data;
            if (resultObj.code == 0 && resultObj.data != null) {
              this.token1 = resultObj.data;
              this.axios.get(baseUrl + "/login/send_code?phone=" + phone + "&i=0" + "&userToken=" + this.token1).then((res) => {
                let resultObj = res.data;
                if (resultObj.code == 0) {
                  this.token2 = resultObj.data;
                  this.$refs.timerBtn.start(); //启动倒计时
                } else {
                  this.$vux.toast.text(resultObj.msg, "middle");
                  this.$refs.timerBtn.stop();
                }
              }).catch(function (error) {
                this.$vux.toast.text("网络异常", "middle");
              });
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
            }
          }).catch(function (error) {
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      getIdentity() {//获取identity
        if (this.identity != "" && this.identity != null && this.identity != undefined) {//若存在更新identity，不存在就取缓存
          setSessionStore("upIdentity", this.identity);
          this.identity = getSessionStore("upIdentity")
        }
      },
      register() {//注册
        let phone = this.phone;
        let pwd = this.password;
        let code = this.codeNum;
        if (this.checkPhone() && this.checkPwd() && this.checkCode()) {
          if (!this.choice) {
            this.$vux.toast.text("请阅读并勾选协议", "middle");
            return;
          }
          //校验验证码
          this.axios.get(baseUrl + "/login/mate_code?phone=" + phone + "&i=0&code=" + code + "&token=" + this.token2).then((res) => {
            let mateObj = res.data;
            if (mateObj.code == 0) {
              this.$vux.loading.show({
                text: '加载中',
              });
              //注册
              let data = {"phone": phone, "password": md5(pwd), "type": 0, "upIdentity": this.identity};
              this.axios.post(baseUrl + "/login/add_user", data).then((res) => {
                let resultObj = res.data;
                this.$vux.loading.hide();
                if (resultObj.code == 0 && resultObj.data == 0) {
                  removeSessionStore("account");
                  if (this.id) {
                    this.$router.push({path: '/login', query: {"id": this.id}});
                  } else if (this.interviewId) {
                    this.$router.push({path: '/login', query: {"interviewId": this.interviewId}});
                  } else {
                    this.$router.push('/login');
                  }
                } else {
                  this.$vux.toast.text(resultObj.msg, "middle");
                  return;
                }
              }).catch(() => {
                this.$vux.loading.hide();
                this.$vux.toast.text("网络异常", "middle");
              });
            } else {
              this.$vux.toast.text(mateObj.msg, "middle");
            }
          }).catch(() => {
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      inputBlur() {
        window.scrollTo(0, 0);
      },
      protocol() {//协议
        setSessionStore("account", Base64.encode(this.phone));
        this.$router.push("/protocol");
      }
    },
    components: {
      'timer-btn': code
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../common/stylus/mixin.styl"

  .login-wrap
    wh(100%, 100%)
    overflow: hidden
    .bg-box
      display: flex
      flex-direction: column
      position: relative
      wh(100%, 4.72rem)
      bg-image("./bg.png", 100%, 100%)
      .logo
        margin: 0 auto
        padding-top: 1.267rem
        wh(1rem, 1rem)
      .title
        text-align: center
        margin-top: 0.133rem
        size-color(0.507rem, $color)
        font-weight: bold
      .tab-box
        display: flex
        position: absolute
        left: 0
        right: 0
        bottom: -0.373rem
        margin: auto
        width: 4.64rem
        .btn
          wh(2.667rem, 0.827rem)
          box-shadow: 0rem 1px 0.107rem 0rem #025C9B
          border-radius: 0.413rem
          font-size: 0.44rem
          text-align: center
          line-height: 0.827rem
        .login
          z-index: 1
          left: 0
          color: $color
          background: rgba(255, 255, 255, 1)
        .regist
          position: absolute
          z-index: 2
          right: 0
          color: rgba(255, 255, 255, 1)
          background: linear-gradient(-90deg, rgba(165, 130, 255, 1), rgba(60, 175, 255, 1))
    .regist-box
      width: 100%
      margin-top: 0.8rem
      .wrap-box
        display: flex
        align-items: center
        margin: 0 auto
        width: 82.67%;
        height: 1.133rem
        border-bottom: 1px solid rgba(230, 230, 230, 1)
        margin-bottom: 0.547rem
        .icon1
          width: 0.787rem
          .phone1
            width: 0.413rem
            height: 0.493rem
        .icon3
          width: 0.787rem
          .pwd1
            width: 0.413rem
            height: 0.493rem
        .input-box
          width: auto
          .phone2
            width: 100%
            height: 100%
            font-size: 0.4rem
            color: rgba(169, 169, 169, 1)
        .pwd-box
          width: auto
          .pwd2
            width: 100%
            height: 100%
            font-size: 0.4rem
            color: rgba(169, 169, 169, 1)
        .icon2
          width: 0.533rem
          height: 0.533rem
          margin-left: auto
          .phone3
            width: 100%
            height: 100%
        .icon4
          width: 0.573rem
          height: 0.413rem
          margin-left: auto
          .pwd3
            width: 100%
            height: 100%
        .code-box
          width: 60%
          .code
            width: 100%
            height: 100%
            font-size: 0.4rem
            color: rgba(169, 169, 169, 1)
        .icon5
          width: auto
          height: 0.4rem
          margin-left: auto
          font-size: 0.4rem
          .btn
            line-height: 0.4rem
            text-align: center
            color: rgba(60, 175, 255, 1)
            background: #FFF

      .distance
        margin-bottom: 0.4rem
      .opt-box
        display: flex
        align-items: center
        justify-content: center
        margin: 0 auto
        margin-bottom: 2.5rem
        width: 82.67%
        height: 0.32rem
        font-size: 0.32rem
        .select-box
          width: 0.32rem
          height 0.32rem
          margin-right: 0.2rem
          .checkbox
            width: 100%
            height: 100%
            outline: none
            background: rgba(60, 175, 255, 1)
            border-radius: 0.04rem
            -webkit-appearance: none
          .select-icon
            background: url("../../images/hock.png") no-repeat center rgba(60, 175, 255, 1)
            border-radius: 0.04rem
            background-size: 0.227rem 0.173rem
        .protocol
          color: rgba(109, 109, 109, 1)
      .btn-box
        width: 100%
        height: 1.333rem
        text-align: center
        .sure
          width: 82.67%
          height: 100%
          line-height: 1.333rem
          background: rgba(60, 175, 255, 1)
          border-radius: 0.107rem
          font-size: 0.48rem
          color: rgba(255, 255, 255, 1)
</style>
